//
//  TSSplicingTemplateEditViewController.m
//  TSImageFilterDemo
//
//  Created by ciyouzen on 2017/2/25.
//  Copyright © 2017年 dvlproad. All rights reserved.
//

#import "TSSplicingTemplateEditViewController.h"
#import <Masonry/Masonry.h>
#import "CQTemplateContainerView.h"

#import <CJBaseUIKit/UIImage+CJCreate.h>
#import "TSImageResultViewController.h"

@interface TSSplicingTemplateEditViewController () <CQTemplateContainerViewDelegate> {
    
}
@property (nonatomic, strong) CQTemplateContainerView *templateContentView;          /**< 模板上的所有图片的展示视图 */

@property (nonatomic, strong, readonly) NSArray<UIImage *> *originImages;   /**< 要处理的图片 */
@property (nonatomic, assign, readonly) NSInteger styleIndex;               /**< 图片的类型 */

@end

@implementation TSSplicingTemplateEditViewController


- (instancetype)initWithImages:(NSArray<UIImage *> *)images styleIndex:(NSInteger)styleIndex {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        _originImages = images;
        _styleIndex = styleIndex;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = NSLocalizedString(@"调整图片", nil);
    //self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"返回", nil) style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"裁剪", nil) style:UIBarButtonItemStylePlain target:self action:@selector(clipAndShowResultImage)];
    
    [self setupViews];
    
    [self updateImages:self.originImages styleIndex:self.styleIndex];
}

#pragma mark - setupViews
- (void)setupViews {
    self.view.backgroundColor = [UIColor blackColor];
    
    CQTemplateContainerView *templateContentView = [[CQTemplateContainerView alloc] initWithFrame:CGRectZero initAnimate:CQTemplateInitAnimateFirst];
    [templateContentView setupRealPointBorderLayerCreateBlock:^CAShapeLayer * _Nonnull{
        CAShapeLayer *realPointBorderLayer = [CAShapeLayer layer];
        realPointBorderLayer.fillColor = UIColor.clearColor.CGColor; // 必须透明
        realPointBorderLayer.strokeColor = UIColor.greenColor.CGColor;
        realPointBorderLayer.lineWidth = 20;
        return realPointBorderLayer;
        
    } selfBorderLayerCreateBlock:^CAShapeLayer * _Nonnull(UIRectCorner rectCorner) {
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.strokeColor = UIColor.redColor.CGColor;
        layer.fillColor = UIColor.whiteColor.CGColor;
        layer.lineWidth = 20;
    //    layer.cornerRadius = 3.0;
    //    layer.masksToBounds = NO;
    //    layer.shadowOffset = CGSizeMake(-5, -5); //(0,0)时是四周都有阴影
    //    layer.shadowColor = [UIColor grayColor].CGColor;
    //    layer.shadowOpacity = 0.1;
        return layer;
    }];
    templateContentView.layer.cornerRadius = 60;
//    templateContentView.layer.borderWidth = 2;
//    templateContentView.layer.borderColor = [UIColor greenColor].CGColor;
    templateContentView.layer.masksToBounds = YES;
    templateContentView.delegateMove = self;
    [self.view addSubview:templateContentView];
    [templateContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view).mas_offset(20);
        make.top.mas_equalTo(self.mas_topLayoutGuide).mas_equalTo(20);
        make.bottom.mas_equalTo(self.mas_bottomLayoutGuide).mas_offset(-100);
    }];
    self.templateContentView = templateContentView;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [templateContentView showPathForRectCorner:UIRectCornerTopLeft |
                                                    UIRectCornerTopRight |
                                                    UIRectCornerBottomLeft |
                                                    UIRectCornerBottomRight];
    });
    
    self.view.backgroundColor = UIColor.systemGray5Color;
    // 画圆弧
    CGFloat kViewHeight = 200;
    UIView *showView = [[UIView alloc] initWithFrame:(CGRectMake(0, self.view.frame.size.height-kViewHeight, self.view.frame.size.width, kViewHeight))];
    showView.backgroundColor = UIColor.clearColor;
    [self.view addSubview:showView];
    
    CGFloat kRadian = 35;
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint: CGPointMake(0, kRadian)];
    [bezierPath addQuadCurveToPoint:CGPointMake(self.view.frame.size.width, kRadian) controlPoint:CGPointMake(self.view.frame.size.width/2, -kRadian)];
//    [bezierPath addLineToPoint: CGPointMake(self.view.frame.size.width, showView.frame.size.height)];
//    [bezierPath addLineToPoint: CGPointMake(0, showView.frame.size.height)];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = bezierPath.CGPath;
    layer.strokeColor = UIColor.redColor.CGColor;
    layer.fillColor = UIColor.clearColor.CGColor;
    layer.cornerRadius = 3.0;
    layer.masksToBounds = NO;
    layer.shadowOffset = CGSizeMake(-5, -5); //(0,0)时是四周都有阴影
    layer.shadowColor = [UIColor grayColor].CGColor;
    layer.shadowOpacity = 0.1;
    [showView.layer addSublayer:layer];
}


- (void)updateImages:(NSArray<UIImage *> *)images styleIndex:(NSInteger)styleIndex {
    [self.templateContentView updateImagesByDataModels:images styleIndex:styleIndex];
}


#pragma mark - Event
- (void)goBack {
    [self dismissViewControllerAnimated:YES completion:nil];
}

/// 裁剪并显示结果
- (void)clipAndShowResultImage {
    UIImage *image = [UIImage cj_imageWithView:self.templateContentView opaque:NO];
    UIViewController *viewController = [[TSImageResultViewController alloc] initWithResultImage:image];
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
